/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Table 8 of Appendix
*****************************************************************************************/



clear
set more off
cap log close

cap mat drop _all

// Table 8 (Appendix)


cap program drop tbl2
program define tbl2 
syntax, from(int) to(int) grp(string) case(int)

	use "${wd}/decomp_dynamic/decomp_grp_`grp'_case_`case'_chain_`from'.dta", clear 
	
	keep if inlist(year, `from',`to')
	drop observed  part1 part2 deunion cf2 cf1 cf3 cf_* 
	
	// Overall observed wage trends
	gen observed = W_obs0 * (1 - union_share  ) + W_obs1 * union_share - (W_obs0[1] * (1 - union_share[1]) + W_obs1[1] * union_share[1])

	// part due to changing union proportion
	gen part1   = (union_share - union_share[1]) * (W_obs1 - W_obs0)

	// part due to changing wage differentials
	gen part2   = union_share[1] * (W_obs1 - W_obs0 - (W_obs1[1] - W_obs0[1]))

	// CF 2 - non-union wage trends (ie, holdign union proportion and differential constant)
	gen cf2   = W_obs0 - W_obs0[1] 

	// CF 1 - holding union proportion constant.
	gen cf1 = part2 + cf2

	// Decomposing CF 2 into de-union effect and CF 3	

	// Deunion	
	gen deunion  =  W_obs0 - W_full0

	// CF 3 - full counterfactual
	gen cf3      =  W_full0 - W_obs0[1]

	// full decomp of compsition effect

	gen cf_threat = W_obs0 - W_second0
	gen cf_transition = W_second0 -  W_first0
	gen cf_premia =  W_first0 - W_full0

	keep observed  part1 part2 deunion cf_* 
	order observed part1 part2 deunion  cf_threat cf_transition cf_premia  

	keep if _n==2
	
	mkmat *, matrix(`grp')
	mat `grp' =  `grp''
	mat colnames `grp' = "`grp'"
	
	mata {
		A = st_matrix("`grp'")
		A
		B = sum(A[2..4])
		B
		C = B / A[1] 
		D = A \ B \ C
		st_matrix("`grp'", D)
		}
		
	mat colnames `grp' = "`grp'"
end

cap mat drop outmat
cap mat drop res
foreach var in all { // men women EF_young_low  EF_young_high EF_old_low EF_old_high { 
	
	// Just 1980s
	tbl2, grp(`var')   from(1) to(2) case(6)
	mat res = `var'
	
	// Just 1990s
	tbl2, grp(`var')   from(2) to(3) case(6)
	mat res = res , `var'

	// Just 2010s
	tbl2, grp(`var')   from(3) to(4) case(6)
	mat res = res , `var'

	*matrix colsum = res * J(3,1,1)	
	matrix colsum = res[1...,1] + res[1...,2] + res[1...,3]
	matrix list colsum
	
	matrix colsum = colsum[1..8,1]
	matrix lastrow = colsum[8,1] / colsum[1,1]  // Compute new last row
	matrix colsum = colsum \ lastrow  // Append new row
	matrix list colsum
	
	// chained decomp
	mat outmat = nullmat(outmat) , colsum
	
	// save it
	mat `var' = outmat
	
	// attach it to decadal decomp
	mat outmat = res , outmat
	
	// save for each group
	mat `var'_decomp = outmat
	matrix colnames `var'_decomp = "a" "b" "c" "Total"
	
	mat drop outmat
	
	preserve
	clear
	svmat `var'_decomp, names(col)
	
	renvars ,postfix(_n)
	foreach x in  a b c Total {  
	gen str `x' = string(`x'_n,"%4.3f")
}
drop *_n
	gen str item = ""
local i = 1
foreach x in "(1) Observed" "(2) Union Prop." "(3) Union Premium"  "(4) Non-Union Spillovers" " \ \ (4a) Fixed Threat " " \ \ (4b) Fixed Transitions" " \ \ (4c) Fixed Union Prem. "  "(5) Total " "(6) Total/Observed" {
	replace item = "`x'" if _n == `i'
	local ++i
}
order  item
	
	
	texsave using "${tables}//decomp_ChainV3_`var'.tex",  replace frag autonumber nofix width(15cm)  rowsep(.1cm) varlabels location("h")    ///
		headerlines(" &  1980-90 & 1990-00 & 2000-10 & Total " ) hlines(3 7 8) size(footnotesize) ///
		 footnote(\textbf{\textsc{Notes:}} This table displays results from the decomposition for union and nonunion workers from 1980-1990.  Each column contains the decomposition results for a different subsample. All figures are log changes from 1980 levels.  Details described in main text.    ,width(15cm)   ) ///
		 title(Outside Options Contribution to Changing Wages)
	
	
	restore
	
}


/* Table did not make final version.
mat list all
mat list all_decomp


mat res = all , men  , women , EF_young_low , EF_young_high , EF_old_low , EF_old_high
clear
svmat res, names(col)


renvars ,postfix(_n)

foreach var in all men women EF_young_low  EF_young_high EF_old_low EF_old_high {  
	gen str `var' = string(`var'_n,"%4.3f")
}
drop *_n

gen str item = ""
local i = 1
foreach var in "(1) Observed" "(2) Union Prop." "(3) Union Premium"  "(4) Non-Union Spillovers" " \ \ (4a) Fixed Threat " " \ \ (4b) Fixed Transitions" " \ \ (4c) Fixed Union Prem. "  "(5) Total " "(6) Total/Observed" {
	replace item = "`var'" if _n == `i'
	local ++i
}
order  item

texsave using "${tables}//Table_8_chain_decomp.tex",  replace frag autonumber nofix width(15cm)  rowsep(.1cm) varlabels location("h")    ///
headerlines("&  All & Men & Women & \multicolumn{4}{c}{Men} " "\cmidrule{5-8} " "\addlinespace[-5ex]" "& & & & \footnotesize{Young Low Skill} & Young High Skill & Old Low Skill & Old High Skill " "\cmidrule{2-8} "  "\addlinespace[-5ex]"  "& \multicolumn{7}{c}{1980-1990} " "\addlinespace[-5ex]" ) hlines(3 7 8) size(footnotesize) ///
 footnote(\textbf{\textsc{Notes:}} This table displays results from the decomposition for union and nonunion workers from 1980-1990.  Each column contains the decomposition results for a different subsample. All figures are log changes from 1980 levels.  Details described in main text.    ,width(15cm)   ) ///
 title(Outside Options Contribution to Changing Wages - Subsample Analysis)
*/

 
 /*
 // entire period
tbl2, grp(all) from(1) to(4) case(6)
tbl2, grp(men) from(1) to(4) case(6)
tbl2, grp(women) from(1) to(4) case(6)

// new groups
tbl2, grp(EF_young_low) from(1) to(4) case(6)
tbl2, grp(EF_young_high) from(1) to(4) case(6)
tbl2, grp(EF_old_low) from(1) to(4) case(6)
tbl2, grp(EF_old_high) from(1) to(4) case(6)


mat res = all, men, women, EF_young_low, EF_young_high, EF_old_low, EF_old_high
*/
 
